﻿@using System.Reflection
@using Common;
@model System.Collections.IList
<div>
    <div>
        @Ajax.ActionLink(lang.Create, "Create", null, new AjaxOptions() { UpdateTargetId = "Main" }, new { @class = "btn  btn-default" })
        @Html.ActionLink(lang.Export, null, null, new { report = true, KeywordField = Request["KeywordField"], keyword = Request["keyword"] }, new { @class = "btn  btn-default", target = "_blank" })
        <a class="btn  btn-default" href="javascript:;" onclick="$('#searchform').toggle('blind');">@lang.Search</a>
        @using (Ajax.BeginForm(null, null, new AjaxOptions { UpdateTargetId = "Main", HttpMethod = "get" }, new { id = "searchform", style = "display:none" }))
        {
       
            <div class="form-horizontal">
                @foreach (PropertyInfo title in ViewBag.PropertyInfo)
                {
                    if (title.Name != "Id")
                    {
                    <div class="form-group col-md-4">

                        <label class="col-md-4 control-label">@lang.ResourceManager.GetString(title.Name)</label>
                        <div class="col-md-8">
                            @switch (title.GetMethod.ReturnType.Name)
                            {
                                case "Boolean":
                                
                                <select  name="Search_@title.Name" class="form-control">
                                    <option value="">@lang.All</option>
                                    <option value="true"  @(Request.QueryString["Search_" + @title.Name] == "true" ? "selected" : "")>@lang.True</option>
                                    <option value="false"  @(Request.QueryString["Search_" + @title.Name] == "false" ? "selected" : "")>@lang.False</option>
                                </select>
                        
                                    break;

                                case "Int32":
                                case "Decimal":
                                case "DateTime":
                                <div class="row">
                                    <div class="col-md-6">
                                        @Html.TextBox("Search_" + title.Name, (Request.QueryString["Search_" + title.Name] != null && Request.QueryString["Search_" + title.Name].Contains(",")) ? Request.QueryString["Search_" + title.Name].Split(',')[0] : "", new { size = 10, @class = "form-control" })
                                    </div>

                                    <div class="col-md-6">
                                        @Html.TextBox("Search_" + title.Name, (Request.QueryString["Search_" + title.Name] != null && Request.QueryString["Search_" + title.Name].Contains(",")) ? Request.QueryString["Search_" + title.Name].Split(',')[1] : "", null, new { size = 10, @class = "form-control" })
                                    </div>
                                </div>
                                    break;

                                default:

                                @Html.TextBox("Search_" + title.Name, Request["Search_" + title.Name], new { @class = "form-control" })
                                    break;
                            }
                        </div>
                    </div>
                    }
                }
                <div style="float: left; padding-right: 10px; white-space: nowrap;">
                    <input type="submit" class="btn  btn-default" value="@lang.StartSearch" />
                </div>
            </div>
        }
    </div>
</div>
<div class="table-responsive">
    <table class="table table-hover table-striped table-condensed">
        <thead>
            <tr>
                @foreach (PropertyInfo title in ViewBag.PropertyInfo)
                {
                    if (title.Name == "Id")
                    {
                    <th>
                        @lang.Edit
                    </th>
                    }
                    else
                    {
                    <th>
                        @if (lang.ResourceManager.GetString(title.Name) != null)
                        {
                            if (Request["ordering"] == title.Name)
                            {
                            @Ajax.ActionLink(lang.ResourceManager.GetString(title.Name) + " ↓", null, new { ordering = title.Name + " desc" }, new AjaxOptions { UpdateTargetId = "Main" }) 
                            }
                            else if (Request["ordering"] == title.Name + " desc")
                            {
                            @Ajax.ActionLink(lang.ResourceManager.GetString(title.Name) + " ↑", null, new { ordering = title.Name }, new AjaxOptions { UpdateTargetId = "Main" }) 
                            }
                            else
                            {
                            @Ajax.ActionLink(lang.ResourceManager.GetString(title.Name) + "", null, new { ordering = title.Name }, new AjaxOptions { UpdateTargetId = "Main" }) 
                            }
                        }
                    </th>
                    }
                }
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    @foreach (PropertyInfo title in ViewBag.PropertyInfo)
                    {
                        if (title.Name == "Id")
                        {
                        <td>
                            @Html.Partial("Ajax_Edit_Verify", @item.GetType().GetProperty("Id").GetValue(item, null))
                        </td>
                        }
                        else
                        {
                        <td>
                            @item.GetType().GetProperty(title.Name).GetValue(item, null)
                        </td>
                        }
                    }
                </tr>
            }
        </tbody>
    </table>
</div>
@Html.Raw(Ajax.MikePager((int)Model.GetType().GetProperty("PageIndex").GetValue(Model, null), (int)Model.GetType().GetProperty("PageSize").GetValue(Model, null), (int)Model.GetType().GetProperty("TotalCount").GetValue(Model, null)))